/*
 * Copyright 2014 Jens K., Jalbum AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.package net.jalbum.jprint;
 */
package net.jalbum.areatagging;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import se.datadosen.jalbum.AlbumBean;
import se.datadosen.jalbum.AlbumObject;
import se.datadosen.jalbum.JAlbumFrame;
import se.datadosen.jalbum.JAlbumPlugin;
import se.datadosen.jalbum.Msg;
import se.datadosen.jalbum.PluginContext;
import se.datadosen.util.VersionNumber;

/**
 * Plugin that loads previously stored area tags and allows the user to
 * interactively select picture areas and tag them with additional information.
 */
public class AreaTaggingPlugin implements JAlbumPlugin {

    private static final ImageIcon smallIcon = new ImageIcon(AreaTaggingPlugin.class.getResource("facetagging.png"));
    private PluginContext context;

    private final Action openDialogAction = new AbstractAction(Msg.getString(this, "ui.setAreaTags") + "...", smallIcon) {
        {
            putValue(Action.SHORT_DESCRIPTION, Msg.getString(this, "ui.setAreaTagsToolTip"));
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            VersionNumber installedVersion = new VersionNumber(AlbumBean.getInternalVersion());
            if (installedVersion.compareTo(new VersionNumber("12")) < 0) {
                JAlbumFrame window = context.getJAlbumContext().getFrame();
                JOptionPane.showMessageDialog(window,
                        "We're sorry. This plugin requires jAlbum 12",
                        "Requires jAlbum 12", JOptionPane.ERROR_MESSAGE);
                return;
            }

            AlbumObject[] all = context.getSelectedAlbumObjects();
            int offset = 0;
            for (AlbumObject ao : all) {
                if (ao.getRepresentingAlbumObject() != null) {
                    ao = ao.getRepresentingAlbumObject();
                }
                if (ao.isDisplayableImage()) {
                    JAreaTaggingDialog dialog = new JAreaTaggingDialog(context, ao);
                    if (dialog.proceed) {
                        dialog.setLocationRelativeTo(context.getJAlbumContext().getFrame());
                        dialog.setLocation(dialog.getLocation().x + offset, dialog.getLocation().y);
                        dialog.setVisible(true);
                        offset += 10;
                    } else {
                        dialog.dispose();
                    }
                }
            }
        }
    };

    @Override
    public void init(PluginContext context) {
        this.context = context;
        context.addContextMenuItem(new JMenuItem(openDialogAction));
    }

    @Override
    public boolean onExit() {
        return true;
    }
}
